<template>
    <div class="links">

        <el-container>
            <el-main>
                <h2>友情链接</h2>
                <div class="content">
                    <el-card v-for="(item, index) in links" :key="index" shadow="hover">
                        <a :href="item.href" target="_blank">
                            <img :src="'https://dh.reinness.com/favicon/get.php?url=' + item.href" class="site_icon">
                            <span>{{ item.name }}</span>
                        </a>
                        <p>{{ item.desc != null ? item.desc : item.name }}</p>
                    </el-card>
                </div>

            </el-main>
            <el-aside width="25%">

                <el-form
                    ref="ruleForm" 
                    :model="ruleForm" 
                    :rules="rules" 
                    label-position="top" 
                    size="small"
                    label-width="100px" 
                    class="link-form"
                >
                    <h4>友链申请</h4>
                    <el-form-item label="站点名称" prop="name">
                        <el-input v-model="ruleForm.name" placeholder="请输入站点名称"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱地址" prop="email">
                        <el-input v-model="ruleForm.email" placeholder="请输入有效的邮箱地址"></el-input>
                    </el-form-item>
                    <el-form-item label="站点地址" prop="url">
                        <el-input v-model="ruleForm.url" placeholder="https://或者http://"></el-input>
                    </el-form-item>
                    <el-form-item label="站点描述" prop="desc">
                        <el-input v-model="ruleForm.desc" type="textarea" placeholder="请一句话描述一下您的站点"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" :disabled="button_status == -1" @click="submitForm('ruleForm')">立即申请
                        </el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
                <div class="tips">
                    <p><i class="iconfont icon-tips"></i> 温馨提示: </p>
                    <p>1、在申请本站友链之前请确保已将本站加入友链。</p> 
                    <p>2、本站优先录入同类原创、技术生活类博客。任何包含违法国家法律或不健康内容站点均不录入。</p>
                    <p>3、百度、搜狗等搜索引擎权重无要求，但是要求时常更新网站内容。</p>
                    <p>4、本站会不定期互相走访友链，发现单方取消友链便拉入黑名单。</p>
                    <p>特别提醒: 若无意外，在申请友链后24小时内完成审核并录入站点，如超时还未审核完成，请留言或者私信给我。审核结果均以邮件形式通知，请输入常用有效邮箱。 </p>
                </div>
            </el-aside>
        </el-container>
    </div>
</template>
<script>
export default {
    name: 'LinkDom',
    data() {
        return {
            links: [],
            ruleForm: {
                name: '',
                email: '',
                url: '',
                desc: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入站点名称', trigger: 'blur' },
                    { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
                ],
                url: [
                    { required: true, message: '请输入站点地址', trigger: 'blur' },
                    { type: 'url', message: '请输入正确的站点地址', trigger: 'blur,change' }
                ],
                desc: [
                    { required: true, message: '请输入站点描述', trigger: 'blur' },
                    { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur,change' }
                ]
            },
            button_status: 0
        }
    },
    mounted() {
        // 拉取该文件夹下所有文件信息
        this.getLinkList()
    }, methods: {
        getLinkList() {
            this.$Api.getLinkList().then(res => {
                this.links = res.data
            })
        },
        submitForm(formName) {
            this.button_status = -1;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$Api.applyLink(this.ruleForm).then(res => {
                        if (res.code !== 200) {
                            this.$message({
                                message: res.msg,
                                type: 'error',
                                offset: 100
                            });
                        } else {
                            this.$message({
                                message: res.msg,
                                type: 'success',
                                offset: 100
                            });
                            this.resetForm('ruleForm');
                        }
                        this.button_status = 0;
                    })
                } else {
                    this.button_status = 0;
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>
<style lang="less" scoped>
.links {
    h2 {
        text-align: center;
        line-height: 100px;
        color: #555;
        font-weight: 500;
    }

    .content {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;

        .el-card {
            margin: 0 10px 20px;
            width: 22.9%;
            box-sizing: border-box;
            text-align: left;
            background-color: unset;

            a {
                display: block;
                // padding: 0 20px;

                img {
                    width: 15px;
                    height: 15px;
                    display: inline-block;
                    vertical-align: middle;
                }

            }

            p {
                clear: both;
                display: -webkit-box;
                overflow: hidden;
                margin-top: 1rem;
                height: 60px;
                text-overflow: ellipsis;
                font-size: 13px;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 3;
                line-height: 1.5;
            }

        }
    }

    .link-form {
        h4 {
            width: 100%;
            color: #409EFF;
            padding: 60px 0 10px 0;
        }

        .el-form-item {
            :deep(.el-form-item__label) {
                padding-bottom: 0px !important;
            }
        }

    }
    .tips{
        line-height: 2;
        padding: 10px 0 20px 0;
        margin: 0 0 20px 0;
        font-size: 12px;
        .icon-tips{
            font-size: 14px;
            color: #f00;
            font-weight: bold;
        }
            
    }
        

}
</style>